class _CommonUtils {
    private static _instance: _CommonUtils = null!;
    static get instance() {
        return this._instance || (this._instance = new _CommonUtils);
    }

    formatDate(format: string, input: Date) {
        let date: { [key: string]: number } = {
            "M+": input.getMonth() + 1,
            "d+": input.getDate(),
            "h+": input.getHours(),
            "m+": input.getMinutes(),
            "s+": input.getSeconds(),
            "q+": Math.floor((input.getMonth() + 3) / 3),
            "S+": input.getMilliseconds()
        };

        let replaceYear = function () {
            return input.getFullYear().toString();
        }
        format = format.replace(/(y+)/i, replaceYear)

        for (let k in date) {
            format = format.replace(new RegExp(`(${k})`), (subFormat) => {
                let value = date[k];
                let str = subFormat.length == 1 ? `${value}` : `00${value}`.substring(value.toString().length);
                return str;
            })
        }
        return format;
    };


    /**@description 获取当前的日期 */
    get date() {
        let date = new Date();
        return this.formatDate("yyyyMMddhhmmss", date);
    }

    /**
    * @description 数组去重
    */
    uniqueArray(arr: any[]) {
        return arr.filter(function (item, pos) {
            return arr.indexOf(item) == pos;
        })
    }
}

export const CommonUtils = _CommonUtils.instance;